package util

import (
	"context"
	"github.com/lib/pq"
	"go.uber.org/zap"
)

// PgErrorChecker check grpc error message
type PgErrorChecker struct {
	slog *zap.SugaredLogger
}

// NewPgErrorChecker initialization
func NewPgErrorChecker(
	slog *zap.SugaredLogger,
) *PgErrorChecker {
	return &PgErrorChecker{
		slog: slog.With("module", "PgErrorChecker"),
	}
}

// FriendlyError go func 执行
func (s *PgErrorChecker) ExtractCode(ctx context.Context, err *error) (pgCode string) {
	if pgerr, ok := (*err).(*pq.Error); ok {
		pgCode = string(pgerr.Code)
	}
	return
}
